// pages/applyScale/updateProfile.js
var app = getApp()
let util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    accountType:"c",
    addr:"",
    code:"",
    tel:"",
    name:"",
    sign:"",
    weChat:""
  },
  bindKeyInput:function(e){
    this.setData({
      [e.currentTarget.dataset.id]:e.detail.value
    })
  },
  toUpdateNumber(){
    wx.navigateTo({
      url:"../updateNumber/updateNumber"
    })
  },
  bindSubmit:function(){
    let that = this;
    let param ={}
    //b端用户业务员
    wx.showLoading({
      title:"正在提交",
      mask:true
    })
    if(this.data.accountType=="b"){
      param={
        "weChat": this.data.weChat,
        "mobile": this.data.tel,
        "signature":this.data.sign
      }
      util.post({
        url: 'https://groomtea.icebartech.com/api/miniApp/salesman/updateDealerDetail', 
        data: param,
        header: {
          "Accept": "*/*"
        },
        success(res) {
          app.globalData.userInfo.weChat = that.data.weChat;
          app.globalData.userInfo.signature = that.data.sign;
          if(app.globalData.tag == true){
            wx.hideLoading()
            wx.clearStorage();
            wx.reLaunch({
              url: "../login/login"
            });
          }else{
            wx.hideLoading()
            wx.setStorageSync("userInfo", app.globalData.userInfo)
            wx.switchTab({
              url: "../mine/mine"
            })
          }
         
          app.titleToast(res.msg); 
        }
      })
    }else //c端用户经销商
    if(this.data.accountType=="c"){
      param={
        "address": this.data.addr,
        "code": app.globalData.userInfo.tempCode,
        "dealerName": this.data.name,
        "mobile": this.data.tel
      }
      console.log(param)
      util.post({
        url: 'https://groomtea.icebartech.com/api/miniApp/dealer/updateDealerDetail', 
        data: param,
        header: {
          "Accept": "*/*"
        },
        success:(res)=> {
          console.log(res);
          app.globalData.userInfo.nickname = that.data.name;
          app.globalData.userInfo.address = that.data.addr;
          if(app.globalData.tag == true){
            wx.clearStorage();
            wx.reLaunch({
              url: "../login/login"
            });
          }else{
            wx.setStorageSync("userInfo", app.globalData.userInfo)
            wx.switchTab({
              url: "../mine/mine"
            })
          }
          app.titleToast(res.msg); 
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const user = app.globalData.userInfo;
    this.setData({
      addr:user.address,
      tel:user.mobile,
      name:user.nickname,
      sign:user.signature||"",
      weChat:user.weChat||"",
      code:user.tempCode||"",
    })
    console.log(user);
    if (!user.dealerType){
       this.setData({
        accountType:'b',
       })
    };
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})